﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace NNDC
{
    public partial class Login : Form
    {
        public Login()
        {
            //设置label透明
            InitializeComponent();
            label1.BackColor = Color.Transparent;
            label2.BackColor = Color.Transparent;
            label3.BackColor = Color.Transparent;
            label4.BackColor = Color.Transparent;
            label5.BackColor = Color.Transparent;
        }

        private void btn_submit_Click(object sender, EventArgs e)
        {
            if (txtName.Text == "")//判断用户名是否为空
            {
                MessageBox.Show(
                    "请输入用户名", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                if (txtpwd.Text == "")//判断密码是否为空
                {
                    MessageBox.Show(
                        "请输入密码", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    SqlConnection conn = data.DBConn.Cycon();//创建数据库连接
                    conn.Open();//打开连接
                    SqlCommand cmd =
                        new SqlCommand("select count(*) from tb_User where UserName='" + txtName.Text + "'and UserPwd='" + txtpwd.Text + "'", conn);//创建sql语句
                   int i = Convert.ToInt32(cmd.ExecuteScalar());//运行sqlyuju
                   if (i > 0)//判断是否存在输入的用户
                   {
                       cmd = new SqlCommand("select * from tb_User where UserName='" + txtName.Text + "'", conn);//查找用户信息语句
                       SqlDataReader sdr = cmd.ExecuteReader();
                       sdr.Read();
                       string UserPower = sdr["power"].ToString().Trim();//取出权限信息
                       conn.Close();
                       Main main = new Main();//赋值并跳转
                       main.power = UserPower;
                       main.Names = txtName.Text;
                       main.Times = DateTime.Now.ToShortDateString();
                       main.Show();
                       this.Hide();
                   }
                   else
                   {
                       MessageBox.Show("用户名和密码错误");
                   }
                }
            }
        }

        private void txtpwd_KeyPress(object sender, KeyPressEventArgs e)
        {
            if(e.KeyChar==13){
                btn_submit_Click(sender, e);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            txtName.Text = "";
            txtpwd.Text = "";
        }
    }
}
